package map.android.baidu.carpool.preferences;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;
import map.android.baidu.carpool.model.CarpoolPosition;
import map.android.baidu.carpool.response.HomePageBaseInfoResponse;
import map.android.baidu.carpool.response.MainConfigResponse;
import map.android.baidu.carpool.response.MainInfoResponse;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class CarpoolSharedPreferences {
	public static final String LOGTAG = "CarpoolSharedPreferences";

	private static final String SHARED_PREFERENCES_NAME = "carpool_component_shared_preferences";

	private static final String IS_FRIST_USE = "is_frist_use";
	private static final String IS_FRIST_USE_FLAG = "is_frist_use_flag";

	private static final String ADD_SHORTCUT_TIP = "add_shortcut_tip";
	private static final String ADD_SHORTCUT_TIP_FLAG = "add_shortcut_tip_flag";

	private static final String MAIN_CONFIG = "main_config";
	private static final String MAIN_CONFIG_RESPONSE = "main_config_response";

    private static final String HOME_BASEINFO = "home_baseinfo";
    private static final String HOME_BASEINFO_RESPONSE = "home_baseinfo_response";

    private static final String MAIN_INFO = "mian_info";
    private static final String MAIN_INFO_RESPONSE = "main_info_response";

    private static final String HOME_GEO_ADDR = "home_geo_addr";
    private static final String HOME_GEO_ADDR_INFO = "home_geo_addr_info";

    private static final String COMPANY_GEO_ADDR = "company_geo_addr";
    private static final String COMPANY_GEO_ADDR_INFO = "company_geo_addr_info";

    private static final String LATEST_START_PLACE = "latest_start_place";
    private static final String LATEST_START_PLACE_INFO = "latest_start_place_info";

    private static final String LATEST_END_PLACE = "latest_end_place";
    private static final String LATEST_END_PLACE_INFO = "latest_end_place_info";

	// 获取入口界面加载的城市列表数据:MainConfig
	public static MainConfigResponse getCacheMainConfigResponse(Activity activity) {
		try {
			if (activity == null) {
				return null;
			}
			SharedPreferences sharedPreferences = activity
					.getSharedPreferences(MAIN_CONFIG, Context.MODE_PRIVATE);
			if (sharedPreferences.contains(MAIN_CONFIG_RESPONSE)) {
				String str = sharedPreferences.getString(MAIN_CONFIG_RESPONSE,
						"");
				if (str.length() <= 0)
					return null;
				Object obj = null;
				byte[] mobileBytes = Base64.decode(str.toString().getBytes(),
						Base64.DEFAULT);
				ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
						mobileBytes);
				ObjectInputStream objectInputStream;
				objectInputStream = new ObjectInputStream(byteArrayInputStream);
				obj = objectInputStream.readObject();
				objectInputStream.close();
				return (MainConfigResponse) obj;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	public static void setCacheMainConfigResponse(Activity activity,
                                            MainConfigResponse content) {
		try {
			if(activity == null)
				return;
			SharedPreferences sharedPreferences = activity
					.getSharedPreferences(MAIN_CONFIG, Context.MODE_PRIVATE);
			Editor editor = sharedPreferences.edit();
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(
					byteArrayOutputStream);
			objectOutputStream.writeObject(content);
			String objString = new String(Base64.encode(
					byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
			editor.putString(MAIN_CONFIG_RESPONSE, objString).commit();
			objectOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

    // 用户信息
    public static HomePageBaseInfoResponse getCacheBaseInfoResponse(Activity activity) {
        try {
            if (activity == null) {
                return null;
            }
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(HOME_BASEINFO, Context.MODE_PRIVATE);
            if (sharedPreferences.contains(HOME_BASEINFO_RESPONSE)) {
                String str = sharedPreferences.getString(HOME_BASEINFO_RESPONSE,
                        "");
                if (str.length() <= 0)
                    return null;
                Object obj = null;
                byte[] mobileBytes = Base64.decode(str.toString().getBytes(),
                        Base64.DEFAULT);
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        mobileBytes);
                ObjectInputStream objectInputStream;
                objectInputStream = new ObjectInputStream(byteArrayInputStream);
                obj = objectInputStream.readObject();
                objectInputStream.close();
                return (HomePageBaseInfoResponse) obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public static void setCacheBaseInfoResponse(Activity activity,
                                                HomePageBaseInfoResponse content) {
        try {
            if(activity == null)
                return;
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(HOME_BASEINFO, Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    byteArrayOutputStream);
            objectOutputStream.writeObject(content);
            String objString = new String(Base64.encode(
                    byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            editor.putString(HOME_BASEINFO_RESPONSE, objString).commit();
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取入口界面加载的城市列表数据:MainInfo
    public static MainInfoResponse getCacheMainInfoResponse(Activity activity) {
        try {
            if (activity == null) {
                return null;
            }
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(MAIN_INFO, Context.MODE_PRIVATE);
            if (sharedPreferences.contains(MAIN_INFO_RESPONSE)) {
                String str = sharedPreferences.getString(MAIN_INFO_RESPONSE,
                        "");
                if (str.length() <= 0)
                    return null;
                Object obj = null;
                byte[] mobileBytes = Base64.decode(str.toString().getBytes(),
                        Base64.DEFAULT);
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        mobileBytes);
                ObjectInputStream objectInputStream;
                objectInputStream = new ObjectInputStream(byteArrayInputStream);
                obj = objectInputStream.readObject();
                objectInputStream.close();
                return (MainInfoResponse) obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public static void setCacheMainInfoResponse(Activity activity,
                                                MainInfoResponse content) {
        try {
            if(activity == null)
                return;
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(MAIN_INFO, Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    byteArrayOutputStream);
            objectOutputStream.writeObject(content);
            String objString = new String(Base64.encode(
                    byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            editor.putString(MAIN_INFO_RESPONSE, objString).commit();
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取入口界面加载的城市列表数据:家和公司
    public static CarpoolPosition getHomeGeoAddrInfo(Activity activity) {
        try {
            if (activity == null) {
                return null;
            }
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(HOME_GEO_ADDR, Context.MODE_PRIVATE);
            if (sharedPreferences.contains(HOME_GEO_ADDR_INFO)) {
                String str = sharedPreferences.getString(HOME_GEO_ADDR_INFO,
                        "");
                if (str.length() <= 0)
                    return null;
                Object obj = null;
                byte[] mobileBytes = Base64.decode(str.toString().getBytes(),
                        Base64.DEFAULT);
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        mobileBytes);
                ObjectInputStream objectInputStream;
                objectInputStream = new ObjectInputStream(byteArrayInputStream);
                obj = objectInputStream.readObject();
                objectInputStream.close();
                return (CarpoolPosition) obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public static void setHomeGeoAddrInfo(Activity activity,
                                          CarpoolPosition content) {
        try {
            if(activity == null)
                return;
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(HOME_GEO_ADDR, Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    byteArrayOutputStream);
            objectOutputStream.writeObject(content);
            String objString = new String(Base64.encode(
                    byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            editor.putString(HOME_GEO_ADDR_INFO, objString).commit();
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取入口界面加载的城市列表数据:家和公司
    public static CarpoolPosition getCompanyGeoAddrInfo(Activity activity) {
        try {
            if (activity == null) {
                return null;
            }
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(COMPANY_GEO_ADDR, Context.MODE_PRIVATE);
            if (sharedPreferences.contains(COMPANY_GEO_ADDR_INFO)) {
                String str = sharedPreferences.getString(COMPANY_GEO_ADDR_INFO,
                        "");
                if (str.length() <= 0)
                    return null;
                Object obj = null;
                byte[] mobileBytes = Base64.decode(str.toString().getBytes(),
                        Base64.DEFAULT);
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        mobileBytes);
                ObjectInputStream objectInputStream;
                objectInputStream = new ObjectInputStream(byteArrayInputStream);
                obj = objectInputStream.readObject();
                objectInputStream.close();
                return (CarpoolPosition) obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public static void setCompanyGeoAddrInfo(Activity activity,
                                             CarpoolPosition content) {
        try {
            if(activity == null)
                return;
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(COMPANY_GEO_ADDR, Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    byteArrayOutputStream);
            objectOutputStream.writeObject(content);
            String objString = new String(Base64.encode(
                    byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            editor.putString(COMPANY_GEO_ADDR_INFO, objString).commit();
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取入口界面加载的城市列表数据:家和公司
    public static CarpoolPosition getLatestStartPlaceInfo(Activity activity) {
        try {
            if (activity == null) {
                return null;
            }
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(LATEST_START_PLACE, Context.MODE_PRIVATE);
            if (sharedPreferences.contains(LATEST_START_PLACE_INFO)) {
                String str = sharedPreferences.getString(LATEST_START_PLACE_INFO,
                        "");
                if (str.length() <= 0)
                    return null;
                Object obj = null;
                byte[] mobileBytes = Base64.decode(str.toString().getBytes(),
                        Base64.DEFAULT);
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        mobileBytes);
                ObjectInputStream objectInputStream;
                objectInputStream = new ObjectInputStream(byteArrayInputStream);
                obj = objectInputStream.readObject();
                objectInputStream.close();
                return (CarpoolPosition) obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public static void setLatestStartPlaceInfo(Activity activity,
                                             CarpoolPosition content) {
        try {
            if(activity == null)
                return;
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(LATEST_START_PLACE, Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    byteArrayOutputStream);
            objectOutputStream.writeObject(content);
            String objString = new String(Base64.encode(
                    byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            editor.putString(LATEST_START_PLACE_INFO, objString).commit();
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取入口界面加载的城市列表数据:家和公司
    public static CarpoolPosition getLatesEndPlaceInfo(Activity activity) {
        try {
            if (activity == null) {
                return null;
            }
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(LATEST_END_PLACE, Context.MODE_PRIVATE);
            if (sharedPreferences.contains(LATEST_END_PLACE_INFO)) {
                String str = sharedPreferences.getString(LATEST_END_PLACE_INFO,
                        "");
                if (str.length() <= 0)
                    return null;
                Object obj = null;
                byte[] mobileBytes = Base64.decode(str.toString().getBytes(),
                        Base64.DEFAULT);
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        mobileBytes);
                ObjectInputStream objectInputStream;
                objectInputStream = new ObjectInputStream(byteArrayInputStream);
                obj = objectInputStream.readObject();
                objectInputStream.close();
                return (CarpoolPosition) obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    public static void setLatestEndPlaceInfo(Activity activity,
                                               CarpoolPosition content) {
        try {
            if(activity == null)
                return;
            SharedPreferences sharedPreferences = activity
                    .getSharedPreferences(LATEST_END_PLACE, Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    byteArrayOutputStream);
            objectOutputStream.writeObject(content);
            String objString = new String(Base64.encode(
                    byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            editor.putString(LATEST_END_PLACE_INFO, objString).commit();
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	public static boolean isFristUse(Activity activity) {
		if (activity == null) {
			return false;
		}
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				IS_FRIST_USE, Context.MODE_PRIVATE);
		boolean flag = sharedPreferences.getBoolean(IS_FRIST_USE_FLAG, true);
		return flag;
	}

	public static void setAlreadyAddShortcutTip(Activity activity, boolean flag) {
		if (activity == null) {
			return;
		}

		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				ADD_SHORTCUT_TIP, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putBoolean(ADD_SHORTCUT_TIP_FLAG, flag);
		editor.commit();
	}

	public static void setIsFristUse(Activity activity, boolean flag) {
		if (activity == null) {
			return;
		}
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				IS_FRIST_USE, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putBoolean(IS_FRIST_USE_FLAG, flag);
		editor.commit();
	}

	public static boolean hasVisitedSetting(Activity activity) {
		if (activity == null) {
			return false;
		}
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		boolean flag = sharedPreferences.getBoolean("car_has_visit_setting",
				false);
		return flag;
	}

	public static void setHasVisitedSetting(Activity activity, boolean visited) {
		if (activity == null) {
			return;
		}
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putBoolean("car_has_visit_setting", visited);
		editor.commit();
	}

	public static boolean hasEntranceIntroduce(Activity activity) {
		if (activity == null) {
			return false;
		}
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		boolean flag = sharedPreferences.getBoolean(
				"car_has_entrance_introduce", false);
		return flag;
	}

	public static void sethasEntranceIntroduce(Activity activity,
			boolean visited) {
		if (activity == null) {
			return;
		}
		SharedPreferences sharedPreferences = activity.getSharedPreferences(
				SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putBoolean("car_has_entrance_introduce", visited);
		editor.commit();
	}

}
